package com.tang.thepath;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.util.LevelUtil;

import java.util.ArrayList;
import java.util.List;

public class UserListActivity extends BaseActivity implements View.OnClickListener{
    private List<RequestModel.User> dataList = new ArrayList<>();
    private UserListActivity.ListAdapter adapter;
    private ListView listView;
    //private int page = 1;
    private int level = 0;
    private int currentRole = 0;
    private boolean hasMore = true;
    private int pageSize = 20;
    private int lastItem = 0;

    private Button btnLevel3;
    private Button btnLevel4;
    private Button btnLevel1;
    private Button btnLevel2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);

        //销售和售后
        btnLevel3 = (Button) findViewById(R.id.btn_level3);
        btnLevel4 = (Button) findViewById(R.id.btn_level4);

        btnLevel1 = (Button) findViewById(R.id.btn_level1);
        btnLevel2 = (Button) findViewById(R.id.btn_level2);

        setActButton(btnLevel3);

        btnLevel3.setOnClickListener(this);
        btnLevel4.setOnClickListener(this);
        btnLevel1.setOnClickListener(this);
        btnLevel2.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        adapter = new UserListActivity.ListAdapter();

        listView.setAdapter(adapter);
    }

    private void defaultBtnStyle(){
        btnLevel1.setBackgroundColor(Color.LTGRAY);
        btnLevel2.setBackgroundColor(Color.LTGRAY);
        btnLevel3.setBackgroundColor(Color.LTGRAY);
        btnLevel4.setBackgroundColor(Color.LTGRAY);
        btnLevel1.setTextColor(Color.DKGRAY);
        btnLevel2.setTextColor(Color.DKGRAY);
        btnLevel3.setTextColor(Color.DKGRAY);
        btnLevel4.setTextColor(Color.DKGRAY);
    }

    private void setActButton(Button btn){
        defaultBtnStyle();
        btn.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        btn.setTextColor(getResources().getColor(R.color.white));
    }

    @Override
    protected void onResume() {
        super.onResume();
        hasMore = true;
        dataList.clear();
        level = 0;
        //this.loadPage(level,currentRole);
        //加载全部成员
        this.loadPageAllMember();
    }


    protected void loadPage(int level,int role){
        showLoading("正在加载");
        dataList.clear();
        BizService.listUser(level,role).subscribe(new HttpSubscriber<RequestModel.ListUserResponse>() {
            @Override
            protected void onSuccess(RequestModel.ListUserResponse response) {
                dismisLoading();
                dataList.addAll(response.userList);
                adapter.notifyDataSetChanged();
            }
        });
    }

    protected void loadPageAllMember(){
        showLoading("正在加载");
        dataList.clear();
        BizService.getAllMember().subscribe(new HttpSubscriber<RequestModel.ListUserResponse>() {
            @Override
            protected void onSuccess(RequestModel.ListUserResponse response) {
                dismisLoading();
                dataList.addAll(response.userList);
                adapter.notifyDataSetChanged();
            }
        });
    }



    @Override
    public void onClick(View v) {
        Button btn = (Button) v;

        setActButton(btn);
        int level = Integer.parseInt(btn.getTag().toString());
        this.level = level;
        if(level == 2 || level == 1){
            currentRole = 0;
        }else if(level == 0){

            if(btn.getText().equals("销售")){
                currentRole = 0;
            }else if(btn.getText().equals("售后")){
                currentRole = 1;
            }
        }
        loadPage(this.level,currentRole);

    }


    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int i) {
            return dataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return  dataList.get(i) != null ? dataList.get(i).id : 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            UserListActivity.ViewHolder holder = null;
            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.item_problem,null);
                /*if(position == (customInfoList.size()+1)){
                    convertView = inflater.inflate(R.layout.item_custom_info,null);
                }else{
                    convertView = inflater.inflate(R.layout.item_custom_info,null);
                }*/


                holder = new UserListActivity.ViewHolder();
                holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
                holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
                //holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
                convertView.setTag(holder);

            }else{
                holder = (UserListActivity.ViewHolder)convertView.getTag();
            }

            RequestModel.User obj =(RequestModel.User) getItem(position);
            holder.tv1.setText(obj.name);
            holder.tv2.setText(obj.email);
            //holder.tv3.setText(LevelUtil.getLevelText(obj.level));
            return convertView;
        }
    }

    class ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
    }
}
